package service

import (
	"fmt"
	_ "strings"
	"xueyuan_backend/model"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

//数据库配置
const (
	userName = "ASUS"
	password = "nicaine2233"
	ip       = "127.0.0.1"
	port     = "5432"
	dbName   = "postgre"
)

var db *gorm.DB

func ConnectDb() *gorm.DB {
	// dsn := "host=127.0.0.1 port=5432 user=ASUS dbname=postgres password=nicaine2233 sslmode=disable"
	// db, _ = gorm.Open(postgres.Open(dsn), &gorm.Config{})
	dsn := "host=47.113.220.65 port=5432 user=postgres dbname=postgres password=nicaine2233 sslmode=disable"
	db, _ = gorm.Open(postgres.Open(dsn), &gorm.Config{})

	// if err != nil {
	// 	panic(err)
	// }

	fmt.Println("连接成功")
	return db
}

// SaveUserToDB 添加用户
func SaveUserToDB(user_id string) bool {
	//TODO
	// var db = ConnectDb()
	var user model.User
	user.UserId = user_id
	db.Table("user").Create(&user)

	return true
}

// GetCollectRecordFromDB 用户收藏文章记录
func GetCollectRecordFromDB(user_id string) (articles []model.Article) {
	// var db = ConnectDb()

	// 多表连接
	db.Limit(100).Table("article").Joins("join user_collect on article.article_id = user_collect.article_id").Where("user_collect.user_id", user_id).Find(&articles)

	return articles
}

// SearchArticleFromDB 关键字搜索文章标题
func SearchArticleFromDB(keyword string, page int) (articles []model.Article) {
	// var db = ConnectDb()
	offset := (page - 1) * 20
	db.Table("article").Where("title LIKE ?", "%"+keyword+"%").Order("date desc").Limit(20).Offset(offset).Find(&articles)

	return articles
}

// SearchCollectArticleFromDB 关键字搜索收藏文章标题
func SearchCollectArticleFromDB(keyword string, user_id string) (articles []model.Article) {
	// var db = ConnectDb()
	// 多表连接
	db.Limit(100).Table("article").Joins("join user_collect on article.article_id = user_collect.article_id").Where("user_collect.user_id=? AND article.title LIKE ?", user_id, "%"+keyword+"%").Find(&articles)

	return articles
}

// SearchWebsiteFromDB 关键字搜索网站
func SearchWebsiteFromDB(keyword string) (websites []model.Website) {
	// var db = ConnectDb()
	db.Table("all_website").Where("web_name LIKE ?", "%"+keyword+"%").Find(&websites)

	return websites
}

// AddSubscribeToDB 添加用户订阅网站
func AddSubscribeToDB(user_id string, website_url string) bool {
	// var db = ConnectDb()
	var subscribe model.Subscribe
	subscribe.UserId = user_id
	subscribe.SubUrl = website_url
	db.Table("user_subscribe").Create(&subscribe)

	return true

}

// DeleteSubscribeToDB 删除用户订阅网站
func DeleteSubscribeToDB(user_id string, website_url string) bool {
	// var db = ConnectDb()
	var subscribe model.Subscribe
	subscribe.UserId = user_id
	subscribe.SubUrl = website_url
	db.Table("user_subscribe").Where("user_id=? AND sub_url=?", user_id, website_url).Delete(&subscribe)

	return true

}

// SubscribeViewFromDB 查询用户订阅网站
func SubscribeViewFromDB(user_id string) (websites []model.Website) {
	// var db = ConnectDb()
	db.Table("all_website").Joins("join user_subscribe on all_website.web_url = user_subscribe.sub_url").Where("user_subscribe.user_id", user_id).Find(&websites)

	return websites
}

// ScanArticleFromDB 分栏目浏览文章
func ScanArticleFromDB(user_id string, web_category string, page int) (articles []model.Article) {
	// 用户订阅web_category的网站name
	// var db = ConnectDb()
	var websites []model.Website
	var web_names []string
	db.Table("all_website").Joins("join user_subscribe on all_website.web_url = user_subscribe.sub_url").Where("user_subscribe.user_id=? AND all_website.web_category=?", user_id, web_category).Find(&websites)
	for _, w := range websites {
		web_names = append(web_names, w.WebName)
	}

	offset := (page - 1) * 20
	//返回article表中source_name在name中的文章
	db.Table("article").Where("source_name IN ?", web_names).Order("date desc").Limit(20).Offset(offset).Find(&articles)

	return articles
}

// ViewArticleFromDB 查看文章详情
func ViewArticleFromDB(article_id string) (article model.Article) {
	// var db = ConnectDb()
	db.Table("article").Where("article_id", article_id).Find(&article)

	return article
}

// AddCollectToDB 添加用户收藏文章
func AddCollectToDB(user_id string, article_id string) bool {
	// var db = ConnectDb()
	var collect model.Collect
	collect.UserID = user_id
	collect.ArticleID = article_id
	db.Table("user_collect").Create(&collect)

	return true

}

// DeleteCollectToDB 删除用户收藏文章
func DeleteCollectToDB(user_id string, article_id string) bool {
	// var db = ConnectDb()
	var collect model.Collect
	collect.UserID = user_id
	collect.ArticleID = article_id
	db.Table("user_collect").Where("user_id=? AND article_id=?", user_id, article_id).Delete(&collect)

	return true

}

// AddProfToDB 添加专业及对应默认订阅
func AddProfToDB(user_id string, prof string) bool {
	//用户表添加专业
	// var db = ConnectDb()
	var user model.User
	user.UserId = user_id
	user.Prof = prof

	db.Table("user").Where("user_id", user_id).Save(&user)

	//默认订阅
	var website model.Website
	db.Table("all_website").Where("web_name", prof).Find(&website)
	AddSubscribeToDB(user_id, website.WebUrl)

	return true
}
